package org.dromara.system.service.policy.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.system.domain.policy.Policy;
import org.dromara.system.domain.policy.bo.PolicyBo;
import org.dromara.system.domain.policy.vo.PolicyVo;
import org.dromara.system.mapper.policy.PolicyMapper;
import org.dromara.system.service.policy.IPolicyService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 承保单主Service业务层处理
 *
 * @author Lion Li
 * @date 2025-11-16
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PolicyServiceImpl implements IPolicyService {

    private final PolicyMapper baseMapper;

    /**
     * 查询承保单主
     *
     * @param id 主键
     * @return 承保单主
     */
    @Override
    public PolicyVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询承保单主列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 承保单主分页列表
     */
    @Override
    public TableDataInfo<PolicyVo> queryPageList(PolicyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Policy> lqw = buildQueryWrapper(bo);
        Page<PolicyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的承保单主列表
     *
     * @param bo 查询条件
     * @return 承保单主列表
     */
    @Override
    public List<PolicyVo> queryList(PolicyBo bo) {
        LambdaQueryWrapper<Policy> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Policy> buildQueryWrapper(PolicyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Policy> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(Policy::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getPolicyId()), Policy::getPolicyId, bo.getPolicyId());
        lqw.eq(StringUtils.isNotBlank(bo.getApplicationId()), Policy::getApplicationId, bo.getApplicationId());
        lqw.eq(StringUtils.isNotBlank(bo.getEffectiveTime()), Policy::getEffectiveTime, bo.getEffectiveTime());
        lqw.eq(StringUtils.isNotBlank(bo.getExpireTime()), Policy::getExpireTime, bo.getExpireTime());
        lqw.eq(StringUtils.isNotBlank(bo.getProductCode()), Policy::getProductCode, bo.getProductCode());
        lqw.eq(StringUtils.isNotBlank(bo.getInsuranceCode()), Policy::getInsuranceCode, bo.getInsuranceCode());
        lqw.like(StringUtils.isNotBlank(bo.getInsuranceCodeName()), Policy::getInsuranceCodeName, bo.getInsuranceCodeName());
        lqw.like(StringUtils.isNotBlank(bo.getInsuranceName()), Policy::getInsuranceName, bo.getInsuranceName());
        lqw.eq(StringUtils.isNotBlank(bo.getUserPin()), Policy::getUserPin, bo.getUserPin());
        lqw.eq(StringUtils.isNotBlank(bo.getShopId()), Policy::getShopId, bo.getShopId());
        lqw.like(StringUtils.isNotBlank(bo.getShopName()), Policy::getShopName, bo.getShopName());
        lqw.eq(StringUtils.isNotBlank(bo.getCommodityCode()), Policy::getCommodityCode, bo.getCommodityCode());
        lqw.like(StringUtils.isNotBlank(bo.getCommodityName()), Policy::getCommodityName, bo.getCommodityName());
        lqw.eq(StringUtils.isNotBlank(bo.getBrand()), Policy::getBrand, bo.getBrand());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), Policy::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getReceiveProvinceId()), Policy::getReceiveProvinceId, bo.getReceiveProvinceId());
        lqw.eq(StringUtils.isNotBlank(bo.getMainOrderId()), Policy::getMainOrderId, bo.getMainOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getMerchantId()), Policy::getMerchantId, bo.getMerchantId());
        lqw.eq(StringUtils.isNotBlank(bo.getFirstCategoryId()), Policy::getFirstCategoryId, bo.getFirstCategoryId());
        lqw.eq(StringUtils.isNotBlank(bo.getSecondCategoryId()), Policy::getSecondCategoryId, bo.getSecondCategoryId());
        lqw.like(StringUtils.isNotBlank(bo.getThirdCategoryName()), Policy::getThirdCategoryName, bo.getThirdCategoryName());
        lqw.eq(StringUtils.isNotBlank(bo.getPolicyStatus()), Policy::getPolicyStatus, bo.getPolicyStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getIsReceived()), Policy::getIsReceived, bo.getIsReceived());
        lqw.eq(StringUtils.isNotBlank(bo.getIsCoInsurance()), Policy::getIsCoInsurance, bo.getIsCoInsurance());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessSource()), Policy::getBusinessSource, bo.getBusinessSource());
        lqw.orderByAsc(Policy::getCreateDate);
        lqw.eq(bo.getPremiumReceivable() != null, Policy::getPremiumReceivable, bo.getPremiumReceivable());
        lqw.eq(bo.getInsuredAmount() != null, Policy::getInsuredAmount, bo.getInsuredAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getCoveragePeriod()), Policy::getCoveragePeriod, bo.getCoveragePeriod());
        lqw.eq(StringUtils.isNotBlank(bo.getSubjectQuantity()), Policy::getSubjectQuantity, bo.getSubjectQuantity());
        lqw.eq(bo.getMainCommodityPrice() != null, Policy::getMainCommodityPrice, bo.getMainCommodityPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getSubjectCategoryId()), Policy::getSubjectCategoryId, bo.getSubjectCategoryId());
        lqw.eq(bo.getCommissionRate() != null, Policy::getCommissionRate, bo.getCommissionRate());
        return lqw;
    }

    /**
     * 新增承保单主
     *
     * @param bo 承保单主
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(PolicyBo bo) {
        Policy add = MapstructUtils.convert(bo, Policy.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改承保单主
     *
     * @param bo 承保单主
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(PolicyBo bo) {
        Policy update = MapstructUtils.convert(bo, Policy.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Policy entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除承保单主信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public void saveBatch(List<Policy> list) {
        baseMapper.insertBatch(list);
    }
}
